﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebCore.Models.ViewModels.File;
using System.Configuration;
using System.IO;

namespace WebCore.Models.DataManagers
{
    public class File
    {
        public string Name { get; set; }
    }
    public class Folder
    {
        public string Name { get; set; }
        public List<Folder> SubFolders { get; set; }
        public List<File> Files { get; set; }
        public Folder Parent { get; set; }
        public string Path { get; set; }
    }

    public class FileManager
    {
        public static Folder GetFolder(string rootName, string rootPath, string rootFullPath)
        {
            rootName = rootName ?? ConfigurationManager.AppSettings["File_RootFolder"];
            rootPath = rootPath ?? ConfigurationManager.AppSettings["File_RootFolder"];


            Folder root = new Folder();
            DirectoryInfo rootDirectory = new DirectoryInfo(rootFullPath);
            root.Name = rootDirectory.Name;
            root.Files = new List<Models.DataManagers.File>();
            // parent
            if (rootName == ConfigurationManager.AppSettings["File_RootFolder"])
            {
                root.Path = ConfigurationManager.AppSettings["File_RootFolder"];
                root.Parent = root;
            }
            else
            {
                root.Path = rootPath;
                root.Parent = new Folder();
                root.Parent.Name = rootDirectory.Parent.Name;
                root.Parent.Path = rootPath.Remove(rootPath.LastIndexOf("/" + rootName), rootName.Length + 1);
            }

            // files
            FileInfo[] files = rootDirectory.GetFiles();
            foreach (FileInfo fi in files)
            {
                WebCore.Models.DataManagers.File file = new WebCore.Models.DataManagers.File();
                file.Name = fi.Name;
                root.Files.Add(file);
            }
            // sub directories
            DirectoryInfo[] subDirectories = rootDirectory.GetDirectories();
            root.SubFolders = new List<Folder>();
            foreach (DirectoryInfo di in subDirectories)
            {
                Folder subFolder = new Folder();
                subFolder.Name = di.Name;
                subFolder.Parent = root;
                subFolder.Path = root.Path + "/" + di.Name;
                root.SubFolders.Add(subFolder);
            }
            return root;
        }

        public static bool RenameFile(string fullPath, string oldName, string newName, out string error)
        {
            try
            {
                if (newName.Trim() == string.Empty)
                {
                    error = "EmptyFileName";
                    return false;
                }

                string oldPath = System.IO.Path.Combine(fullPath, oldName);
                string newPath = System.IO.Path.Combine(fullPath, newName);
                System.IO.File.Move(oldPath, newPath);

                error = string.Empty;
                return true;
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return false;
            }
        }

        public static bool RenameFolder(string fullPath, string oldName, string newName, out string error)
        {
            try
            {      
                if (newName.Trim() == string.Empty)
                {
                    error = "EmptyFolderName";
                    return false;
                }

                string oldPath = System.IO.Path.Combine(fullPath, oldName);
                string newPath = System.IO.Path.Combine(fullPath, newName);
                System.IO.Directory.Move(oldPath, newPath);

                error = string.Empty;
                return true;
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return false;
            }
        }

        public static bool DeleteFolder(string rootFullPath, out string error)
        {
            try
            {
                Directory.Delete(rootFullPath);                
                error = string.Empty;
                return true;
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return false;
            }
        }

        public static bool AddFolderName(string name, string rootFullPath, out string error)
        { 
            // validate
            if (name.Trim() == string.Empty)
            {
                error = "EmptyFolderName";
                return false;
            }
            string newPath = System.IO.Path.Combine(rootFullPath, name);
            if (Directory.Exists(newPath))
            {
                error = "ExistedFolder";
                return false;
            }
            try
            {
                DirectoryInfo di = Directory.CreateDirectory(newPath);
                error = string.Empty;
                return true;
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return false;
            }
            
        }

        public static bool DeleteFile(string rootFullPath, string name, out string error)
        {
            try
            {
                string newPath = System.IO.Path.Combine(rootFullPath, name);
                System.IO.File.Delete(newPath);
                error = string.Empty;
                return true;
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return false;
            }
        }
    }
}